package fr.janalyse

import scala.collection.generic.CanBuildFrom

package object series {

  final class RaisedForSeries[N<%Number, C <:Cell](n: N) {
    def *(x: Series[C]): Series[CalcCell] = x * n
    def +(x: Series[C]): Series[CalcCell] = x + n
    def /(x: Series[C]): Series[CalcCell] = x / n
    def -(x: Series[C]): Series[CalcCell] = x - n
  }
  implicit def raiseToSeries[N<%Number, C <:Cell](n: N): RaisedForSeries[N,C] = new RaisedForSeries[N,C](n)

  
  
  
  def align[C<:Cell, A<:Series[C], I[A]<:Iterable[A]] (list:I[A], timeNorm:Long=>Long = x=>x)
           (implicit bf: CanBuildFrom[I[A], A, I[A]], cellBuilder:CellBuilder[C])
           : I[A] = {
    // We'll only take common normalized time for all given series
    val times2alignwith = 
      list
      .map(_.map(c=> timeNorm(c.time) ))
      .reduce(_ intersect _)
      .toSet
    
    val baselist =
      list
      .map(_.take(times2alignwith.head, times2alignwith.last))
      .map(_.sample(timeNorm))
    
    var builder = bf.apply()
	  var prev:Option[A]=None
	  for(series <- list) {
	    val alignedSeries = series
	    builder+=alignedSeries
	  }
	  builder.result
  }
  
  /*  */
}
